/*
 * Copyright (c) Kumo Inc. and affiliates.
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <melon/json/dynamic.h>

#include <melon/benchmark.h>
#include <melon/init/init.h>

using melon::Dynamic;

BENCHMARK(moveBool, iters) {
    for (size_t i = 0; i < iters; ++i) {
        Dynamic b = true;
        melon::makeUnpredictable(b);
        Dynamic other = std::move(b);
        melon::doNotOptimizeAway(other);
    }
}

BENCHMARK(moveShortString, iters) {
    for (size_t i = 0; i < iters; ++i) {
        Dynamic s = "short";
        melon::makeUnpredictable(s);
        Dynamic other = std::move(s);
        melon::doNotOptimizeAway(other);
    }
}

BENCHMARK(moveLongString, iters) {
    for (size_t i = 0; i < iters; ++i) {
        melon::BenchmarkSuspender braces;
        Dynamic s =
                "a very long string which will certainly have to go on the heap";
        melon::makeUnpredictable(s);
        braces.dismiss();
        Dynamic other = std::move(s);
        melon::doNotOptimizeAway(other);
    }
}

BENCHMARK(copyBool, iters) {
    Dynamic b = true;
    melon::makeUnpredictable(b);
    for (size_t i = 0; i < iters; ++i) {
        Dynamic other = b;
        melon::doNotOptimizeAway(other);
    }
}

BENCHMARK(assignRawBool, iters) {
    bool b = true;
    Dynamic other = false;
    melon::makeUnpredictable(b);
    for (size_t i = 0; i < iters; ++i) {
        other = b;
        melon::doNotOptimizeAway(other);
    }
}

BENCHMARK(copyShortString, iters) {
    Dynamic s = "short";
    melon::makeUnpredictable(s);
    for (size_t i = 0; i < iters; ++i) {
        Dynamic other = s;
        melon::doNotOptimizeAway(other);
    }
}

BENCHMARK(copyLongString, iters) {
    melon::BenchmarkSuspender braces;
    Dynamic s = "a very long string which will certainly have to go on the heap";
    melon::makeUnpredictable(s);
    braces.dismiss();
    for (size_t i = 0; i < iters; ++i) {
        Dynamic other = s;
        melon::doNotOptimizeAway(other);
    }
}

BENCHMARK(hashBool, iters) {
    Dynamic b = true;
    melon::makeUnpredictable(b);
    for (size_t i = 0; i < iters; ++i) {
        auto hash = b.hash();
        melon::doNotOptimizeAway(hash);
    }
}

BENCHMARK(hashShortString, iters) {
    Dynamic s = "short";
    melon::makeUnpredictable(s);
    for (size_t i = 0; i < iters; ++i) {
        auto hash = s.hash();
        melon::doNotOptimizeAway(hash);
    }
}

BENCHMARK(hashLongString, iters) {
    melon::BenchmarkSuspender braces;
    Dynamic s = "a very long string which will certainly have to go on the heap";
    melon::makeUnpredictable(s);
    braces.dismiss();
    for (size_t i = 0; i < iters; ++i) {
        auto hash = s.hash();
        melon::doNotOptimizeAway(hash);
    }
}

BENCHMARK(sizeShortString, iters) {
    Dynamic s = "short";
    melon::makeUnpredictable(s);
    for (size_t i = 0; i < iters; ++i) {
        auto sz = s.size();
        melon::doNotOptimizeAway(sz);
    }
}

BENCHMARK(sizeLongString, iters) {
    melon::BenchmarkSuspender braces;
    Dynamic s = "a very long string which will certainly have to go on the heap";
    melon::makeUnpredictable(s);
    braces.dismiss();
    for (size_t i = 0; i < iters; ++i) {
        auto sz = s.size();
        melon::doNotOptimizeAway(sz);
    }
}

int main(int argc, char **argv) {
    melon::Init init(&argc, &argv);
    melon::runBenchmarks();
    return 0;
}
